package com.ruoyi.daily.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.customerService.mapper.CsSignRateUnStandardExamineMapper;
import com.ruoyi.daily.domain.dto.*;
import com.ruoyi.daily.domain.vo.ByDateAndPageVo;
import com.ruoyi.daily.mapper.CollectingMonitorMapper;
import com.ruoyi.daily.mapper.MonitoringStationMapper;
import com.ruoyi.daily.mapper.QualityReportOneMapper;
import com.ruoyi.daily.mapper.WorkOrderManagementMapper;
import com.ruoyi.daily.service.MonitoringStationService;
import com.ruoyi.qualityControl.mapper.QcTimeExamineReturnGoodSheetMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;

/**
 * @Author cmz
 * @Date 2024-06-03 10:20
 */
@Service
public class MonitoringStationServiceImpl implements MonitoringStationService {
    @Resource
    private QualityReportOneMapper qualityReportOneMapper;
    @Resource
    private CsSignRateUnStandardExamineMapper csSignRateUnStandardExamineMapper;
    @Resource
    private QcTimeExamineReturnGoodSheetMapper qcTimeExamineReturnGoodSheetMapper;
    @Resource
    private MonitoringStationMapper monitoringStationMapper;
    @Resource
    private WorkOrderManagementMapper workOrderManagementMapper;
    @Resource
    private CollectingMonitorMapper collectingMonitorMapper;
            
    @Override
    public R<TimelinessMonitoringDto> timelinessMonitoring() {
        TimelinessMonitoringDto dto = new TimelinessMonitoringDto();
        //时效考核
        DeliveryDelayDto deliveryDelayDto = qualityReportOneMapper.deliveryDelay();
        dto.setDeliverBacklog(deliveryDelayDto.getNoTimelyRatio());
        dto.setTimelyRatio(deliveryDelayDto.getTimelyRatio());
        
        //48小时未交货
        FortyEightHRetentionDto fortyEightHRetentionVo = qualityReportOneMapper.fortyEightHRetention();
        dto.setUndelivered(fortyEightHRetentionVo.getFortyEightHRetention());
        
        //签收率
        Double pickupTimelyRate = csSignRateUnStandardExamineMapper.selectPickupTimelyRate();
        dto.setCurrentSigningRate(pickupTimelyRate);
        
        //签收率环比下降
        Double historyPickupTimelyRate = csSignRateUnStandardExamineMapper.selecthistoryPickupTimelyRate();
        dto.setCurrentSigningRateDecline(pickupTimelyRate-historyPickupTimelyRate);//正数为增加，负数为下降
        
        //一阶段签收率
        Double firstSigningRate = csSignRateUnStandardExamineMapper.selectFirstSigningRate();
        dto.setFirstSigningRate(firstSigningRate);
        
        //二阶段签收率
        Double secondSigningRate = csSignRateUnStandardExamineMapper.selectSecondSigningRate();
        dto.setSecondSigningRate(secondSigningRate);
        
        //中心发件量
        Integer numberOfShipments = qcTimeExamineReturnGoodSheetMapper.selectNumberOfShipments();
        dto.setNumberOfShipments(numberOfShipments);
        //网点未操作量
        Integer unhandledQuantity = qcTimeExamineReturnGoodSheetMapper.selectUnhandledQuantity();
        dto.setUnhandledQuantity(unhandledQuantity);
        //回货进度
        Double returnProgress = qcTimeExamineReturnGoodSheetMapper.selectReturnProgress();
        dto.setReturnProgress(returnProgress);
        return R.ok(dto);
    }

    @Override
    public R<OperationQualityDto> operationQuality() {
        OperationQualityDto dto = new OperationQualityDto();
        Integer falseReceipt = monitoringStationMapper.selectFalseReceipt();
        dto.setFalseReceipt(falseReceipt);
        
        //找不到数据来源
        dto.setUnRecovered(0);
        dto.setUnWeighed(0);
        return R.ok(dto);
    }

    @Override
    public R<BranchWorkOrderDto> branchWorkOrder() {
        BranchWorkOrderDto branchWorkOrderDto = new BranchWorkOrderDto();
        Integer processing = monitoringStationMapper.selectProcessingCount();
        branchWorkOrderDto.setProcessing(processing);
        branchWorkOrderDto.setAboutToTimeOut(0);
        branchWorkOrderDto.setOrderPendingCollection(0);
        branchWorkOrderDto.setMinPendingCollection(0);
        return R.ok(branchWorkOrderDto);
    }

    @Override
    public R myWorkOrder() {
        
        return null;
    }

    @Override
    public R<MonitoringStationDto> monitoringStation() {
        MonitoringStationDto monitoringStationDto = new MonitoringStationDto();
        TodayOrderDto todayOrderDto = collectingMonitorMapper.todayOrder();
        monitoringStationDto.setOrderQuantity(todayOrderDto.getOrderQuantity());//订单量
        monitoringStationDto.setReceivedQuantity(todayOrderDto.getReceivedQuantity());//收件量
        monitoringStationDto.setToBeSolicited(collectingMonitorMapper.threeToBeSolicited());//近3天待揽收

        R<TimelinessMonitoringDto> timelinessMonitoringDtoR = this.timelinessMonitoring();
        TimelinessMonitoringDto timelinessMonitoringDto = timelinessMonitoringDtoR.getData();
        
        monitoringStationDto.setDeliverBacklog(timelinessMonitoringDto.getDeliverBacklog());
        monitoringStationDto.setTimelyRatio(timelinessMonitoringDto.getTimelyRatio());
        monitoringStationDto.setUndelivered(timelinessMonitoringDto.getUndelivered());
        monitoringStationDto.setCurrentSigningRate(timelinessMonitoringDto.getCurrentSigningRate());
        monitoringStationDto.setCurrentSigningRateDecline(timelinessMonitoringDto.getCurrentSigningRateDecline());
        monitoringStationDto.setFirstSigningRate(timelinessMonitoringDto.getFirstSigningRate());
        monitoringStationDto.setSecondSigningRate(timelinessMonitoringDto.getSecondSigningRate());
        monitoringStationDto.setNumberOfShipments(timelinessMonitoringDto.getNumberOfShipments());
        monitoringStationDto.setUnhandledQuantity(timelinessMonitoringDto.getUnhandledQuantity());
        monitoringStationDto.setReturnProgress(timelinessMonitoringDto.getReturnProgress());
        
        //操作质量
        monitoringStationDto.setUnWeighed(0);//未称重
        monitoringStationDto.setUnRecovered(monitoringStationMapper.selectUnRecovered());//疑似遗失未找回
        monitoringStationDto.setFalseReceipt(monitoringStationMapper.selectFalseReceipt());
        
        //网点工单
        monitoringStationDto.setNetworkProcessing(monitoringStationMapper.selectNetworkProcessing());
        monitoringStationDto.setNetworkPendingCollection(monitoringStationMapper.selectNetworkPendingCollection());

        //我的工单
        monitoringStationDto.setMyProcessing(monitoringStationMapper.selectMyProcessing());
        monitoringStationDto.setMyAboutToTimeOut(0);
        
        //综合kpi考核
        monitoringStationDto.setSingleTicketIncentive(0.00);
        monitoringStationDto.setPaidDeliveryVolume(0);
        
        //经营看板
        monitoringStationDto.setYesterdayFund(0.00);
        monitoringStationDto.setYesterdayFundSingle(0.00);
        monitoringStationDto.setThisMonthFund(0.00);
        monitoringStationDto.setThisMonthFundSingle(0.00);
        
        //考核指标分析
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime minusDays = now.minusDays(1);

        Date lessDate = Date.from(minusDays.atZone(ZoneId.systemDefault()).toInstant());
        Date nowDate = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        
        monitoringStationDto.setYesterdayDateStr(minusDays.getMonthValue()+1 +"/"+minusDays.getDayOfMonth());
        monitoringStationDto.setYesterdayLossRate(monitoringStationMapper.lossRate(lessDate));
        monitoringStationDto.setYesterdayDamageRate(monitoringStationMapper.selectDamageRate(lessDate));
        monitoringStationDto.setYesterdayCallRate(monitoringStationMapper.selectCallRate(lessDate));
        
        monitoringStationDto.setTodayDateStr(now.getMonthValue()+1 +"/"+now.getDayOfMonth());
        monitoringStationDto.setTodayLossRate(monitoringStationMapper.lossRate(nowDate));
        monitoringStationDto.setTodayDamageRate(monitoringStationMapper.selectDamageRate(nowDate));
        monitoringStationDto.setTodayCallRate(monitoringStationMapper.selectCallRate(nowDate));
        
        //找不到数据来源
        monitoringStationDto.setTimelinessRate(100.00);
        monitoringStationDto.setAccuracyRate(100.00);
        monitoringStationDto.setIntegrityRate(100.00);
        
        //末端发展
        monitoringStationDto.setLossRate(monitoringStationMapper.lossRate(lessDate));
        
        //送货上门
        DeliveryDto deliveryDto = monitoringStationMapper.selectDelivery(lessDate);
        if (deliveryDto==null){
            deliveryDto = new DeliveryDto();
        }
        monitoringStationDto.setRealisticRate(deliveryDto.getRealisticRate());
        monitoringStationDto.setFulfillmentRate(deliveryDto.getFulfillmentRate());
        monitoringStationDto.setCustomerComplaintRate(deliveryDto.getCustomerComplaintRate());
        return R.ok(monitoringStationDto);
    }
}
